package com.bt.framework.decoration

import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView

/**
 *@author:hxm
 *@date: 2025/6/17 23:07
 *@version:
 *@description:普通条目编剧
 */
class NormalItemDecoration : RecyclerView.ItemDecoration() {

    private val mDivider = ColorDrawable()
    private var mLastBottom = false
    private var mIsFirstHead = false

    /**
     * 设置分割线边界
     * @param left 分割线左边界
     * @param top 分割线上边界
     * @param right 分割线右边界
     * @param bottom 分割线下边界
     */
    fun setBounds(left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0) {
        mDivider.bounds.left = left
        mDivider.bounds.top = top
        mDivider.bounds.right = right
        mDivider.bounds.bottom = bottom
    }

    /**
     * 设置分割线颜色
     * @param color 分割线颜色
     */
    fun setColor(@ColorInt color: Int) {
        mDivider.color = color
    }

    /**
     * 设置是否为最后一个条目添加底部间隔
     * @param lastBottom 否为最后一个条目添加底部间隔
     */
    fun setLastBottom(lastBottom: Boolean) {
        mLastBottom = lastBottom
    }

    /**
     * 设置是否为第一个条目添加所有间隔
     * @param isFirstHead 是否为第一个条目添加所有间隔
     */
    fun setIsFirstHead(isFirstHead: Boolean) {
        mIsFirstHead = isFirstHead
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        val lastPosition = state.itemCount - 1
        val position = parent.getChildAdapterPosition(view)

        outRect.left = if (position == 0 && mIsFirstHead) 0 else mDivider.bounds.left
        outRect.top = if (position == 0 && mIsFirstHead) 0 else mDivider.bounds.top
        outRect.right = if (position == 0 && mIsFirstHead) 0 else mDivider.bounds.right
        outRect.bottom = if (position == lastPosition && mLastBottom) mDivider.bounds.bottom else 0
    }

}